package com.example.yls.cnbetaapp.util;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.util.HashMap;
import java.util.Map;

public class ChannelUtils {

    public static Channel getChannel(String connectionDescription) {
        try {
            ConnectionFactory connectionFactory = getConnectionFactory();
            Connection connection = connectionFactory.newConnection(connectionDescription);
            return connection.createChannel();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    private static ConnectionFactory getConnectionFactory() {
        ConnectionFactory factory = new ConnectionFactory();

        factory.setHost("192.168.0.13");
        factory.setPort(5672);
        factory.setUsername("user_admin");
        factory.setPassword("passwd_admin");
        factory.setAutomaticRecoveryEnabled(true);
        factory.setNetworkRecoveryInterval(10000);

        Map<String, Object> connectionFactoryPropertiesMap = new HashMap();
        connectionFactoryPropertiesMap.put("principal", "RobertoHuang");
        connectionFactoryPropertiesMap.put("description", "Cnbeta手机app");
        connectionFactoryPropertiesMap.put("emailAddress", "RobertoHuang@foxmail.com");
        factory.setClientProperties(connectionFactoryPropertiesMap);

        return factory;
    }
}
